<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 * 档口收支
 *
 * @icon fa fa-circle-o
 */
class Zjdangkou extends Backend
{

    /**
     * Zjdangkou模型对象
     * @var \app\admin\model\Zjdangkou
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Zjdangkou;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            //if($input){}
            $list = $this->model
                ->where("")
                //->where('day <= 5')
                ->order('id','asc')
                ->paginate($limit);
            $input = input();
            if(isset($input['filter']) && !empty(json_decode($input['filter'],true))){
                $filter = json_decode($input['filter'],true);
                if(!empty($filter['day'])) {
                    list($s, $e) = explode(' - ', $filter['day']);
                    $daylist = getDatesBetween($s, $e);
                    //$list = $re;
                    $list = [];
                    foreach ($daylist as $k => $item) {
                        $list[$k]['day'] = $item;
                    }
                }
            }else{
                $new_list = [];
                foreach($list as $k1=>$v1){
                    $day = date('Y-m-').$v1->day;
                    $d = date('Y-m-d',strtotime($day));
                    $new_list[$k1]['day'] = $d;
                }
                $list = $new_list;
            }
            $anzhuang_fee = [];
            $xufei_arr = [];
            $banqian_arr = [];
            $zhi_za_arr = [];
            $shou_za_arr = [];
            $ershou_arr = [];

            $shou_arr = [];
            $zhi_arr = [];
            $jie_arr = [];
            $anzhuang_fee_arr = [];
            $dui_arr = [];
            foreach ($list as &$row) {
                $d = $row['day'];

                $ronghe = $this->cacl2($d,'fa_zjxinzhuang');
                $row['ronghe'] = $ronghe;
                $banqian = $this->cacl2($d,'fa_zjfutong');
                $row['banqian'] = $banqian;
                $anzhuang_fee2 = $this->cacl2($d,'fa_zjxinzhuang','anzhuang_fee');
                $row['anzhuang_fee'] = $anzhuang_fee2;
                $xufei = $this->cacl2($d,'fa_zjxufei');
                $row['xufei'] = $xufei;
                $s = $this->cacl2($d,'fa_zjzafeishouru');
                $row['shou_za'] = $s;
                $ershou = $this->cacl2($d,'fa_zjershou');
                $row['ershou'] = $ershou;

                $z = $this->cacl2($d,'fa_zjzafei');
                $row['zhi_za'] = $z;

                $dui_ronghe = $this->duicacl2($d,'fa_zjxinzhuang','ptype');
                $dui_anzhuang_fee2 = $this->duicacl2($d,'fa_zjxinzhuang','ptype','anzhuang_fee');
                $dui_banqian = $this->duicacl2($d,'fa_zjfutong','ptype');
                $dui_xufei = $this->duicacl2($d,'fa_zjxufei','ptype');
                $dui_s = $this->duicacl2($d,'fa_zjzafeishouru','ptype');
                $dui_ershou = $this->duicacl2($d,'fa_zjershou','ptype');

                $duigong = float_two($dui_ronghe+$dui_anzhuang_fee2+$dui_banqian+$dui_xufei+$dui_s+$dui_ershou);
                $row['duigong'] = $duigong;
                $dui_arr[] = $duigong;

                $shou = float_two($ronghe+$banqian+$xufei+$s+$ershou+$anzhuang_fee2-$duigong);
                $row['shou'] = $shou;
                $zhi = float_two($z);
                $row['zhi'] = $zhi;
                $jie = float_two($shou-$zhi);
                $row['jieyu'] = $jie;
                $anzhuang_fee_arr[] = $anzhuang_fee2;
                $anzhuang_fee[] = $ronghe;
                $xufei_arr[] = $xufei;
                $banqian_arr[] = $banqian;
                $zhi_za_arr[] = $z;
                $shou_za_arr[] = $s;
                $ershou_arr[] = $ershou;
                $shou_arr[] = $shou;
                $zhi_arr[] = $zhi;
                $jie_arr[] = $jie;
            }
            if(!empty($list)){
                $firstarr[]=[
                    'ronghe'          => '总计: '.float_two(array_sum($anzhuang_fee)),
                    'anzhuang_fee'          => '总计: '.float_two(array_sum($anzhuang_fee_arr)),
                    'xufei'          => '总计: '.float_two(array_sum($xufei_arr)),
                    'banqian'          => '总计: '.float_two(array_sum($banqian_arr)),
                    'zhi_za'          => '总计: '.float_two(array_sum($zhi_za_arr)),
                    'shou_za'          => '总计: '.float_two(array_sum($shou_za_arr)),
                    'ershou'          => '总计: '.float_two(array_sum($ershou_arr)),
                    'duigong'          => '总计: '.float_two(array_sum($dui_arr)),
                    'shou'          => '总收入: '.float_two(array_sum($shou_arr)),
                    'zhi'          => '总支出: '.float_two(array_sum($zhi_arr)),
                    'jieyu'          => '结余: '.float_two(array_sum($jie_arr)),
                ];
                $list = array_merge($list, $firstarr);
            }

            $result = array("total" => count($list), "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    protected function cacl2($d,$table,$field='price',$type = '')
    {

        if(!empty($type)){
            $tfield = $table == 'fa_ydchuzhuang'?'gtype':'ztype';
            $ronghe = Db::table($table)
                ->where('opt_date',$d)
                ->where($tfield,$type)
                ->whereNull('deletetime')
                ->sum($field);
        }else{
            $ronghe = Db::table($table)
                ->where('opt_date',$d)
                ->whereNull('deletetime')
                //->where('memo',$memo)
                ->sum($field);
        }
        return $ronghe==0?0:float_two($ronghe);
    }

    protected function duicacl2($d,$table,$pfield,$field='price')
    {
        $ronghe = Db::table($table)
            ->where('opt_date',$d)
            ->whereNull('deletetime')
            ->where($pfield,5)
            //->where('memo',$memo)
            ->sum($field);
        return $ronghe==0?0:float_two($ronghe);
    }


    protected function cacl($d,$memo)
    {
        $ronghe = Db::table('fa_zjdaylog')
            ->where('log_date',$d)
            ->where('memo',$memo)
            ->whereNull('deletetime')
            ->sum('money');
        return $ronghe==0?'-':float_two($ronghe);
    }


}
